package org.dromara.business.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.ProductTable;
import org.dromara.business.domain.bo.ProductTableBo;
import org.dromara.business.domain.vo.ProductTableVo;
import org.dromara.business.service.ProductTableService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品
 *
 * @author Lion Li
 * @date 2025-01-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/table")
public class ProductTableController extends BaseController {

    private final ProductTableService productTableService;

    /**
     * 查询产品列表
     */
//    @SaCheckPermission("business:table:list")
    @GetMapping("/list")
    public TableDataInfo<ProductTableVo> list(ProductTableBo bo, PageQuery pageQuery) {
        return productTableService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出产品列表
     */

    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductTableBo bo, HttpServletResponse response) {
        List<ProductTableVo> list = productTableService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品", ProductTableVo.class, response);
    }

    /**
     * 获取产品详细信息
     *
     * @param productId 主键
     */
//    @SaCheckPermission("business:table:query")
    @GetMapping("/{productId}")
    public R<ProductTableVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long productId) {
        return R.ok(productTableService.queryById(productId));
    }

    /**
     * 新增产品
     */
//    @SaCheckPermission("business:table:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductTableBo bo) {
        return toAjax(productTableService.insertByBo(bo));
    }

    /**
     * 修改产品
     */
//    @SaCheckPermission("business:table:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductTableBo bo) {
        return toAjax(productTableService.updateByBo(bo));
    }

    /**
     * 删除产品
     *
     * @param productIds 主键串
     */
//    @SaCheckPermission("business:table:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] productIds) {
        return toAjax(productTableService.deleteWithValidByIds(List.of(productIds), true));
    }

    /**
     * 查询产品库存
     */
    @GetMapping("/repertory")
    public R<List<ProductTable>> getProductTableByIds(){
        List<ProductTable> productTableByIds = productTableService.getProductTableByIds();
        return R.ok(productTableByIds);
    }
    /**
     * 根据产品id查询产品名称
     */

    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/allproducttable")
    public R<List<ProductTableVo>> export( @RequestBody ProductTableBo bo) {
        List<ProductTableVo> list = productTableService.queryList(bo);
        return R.ok(list);
    }
}
